package com.example.keli.ui.register;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.keli.MainActivity;
import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.App;
import com.example.keli.base.AppConfig;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.SPUtils;
import com.example.keli.base.utils.ToastUtil;
import com.google.gson.Gson;
import com.tuo.customview.VerificationCodeView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class VerifyActivity extends BaseActivity {

    @BindView(R.id.close)
    ImageView mClose;
    @BindView(R.id.phone)
    TextView mPhone;
    @BindView(R.id.codeview)
    VerificationCodeView mCodeview;
    @BindView(R.id.time_count)
    TextView mTimeCount;
    String phone = "";
    String code = "";

    public static void start(Context context, String phone) {
        Intent intent = new Intent(context, VerifyActivity.class);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_verify;
    }

    @Override
    protected void bindData() {
        phone = getIntent().getStringExtra("phone");
        mPhone.setText("+86  " + phone);

        mTimeCount.setEnabled(false);
        startCountDown();

        mCodeview.setInputCompleteListener(new VerificationCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
                if(mCodeview.getInputContent().length()==4){
                    code = mCodeview.getInputContent();
                    login();
                }
            }

            @Override
            public void deleteContent() {

            }
        });
    }

    @OnClick({R.id.close, R.id.codeview, R.id.time_count})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.close:
                break;
            case R.id.codeview:
                break;
            case R.id.time_count:
                sendMsg();
                mTimeCount.setEnabled(false);
                break;
        }
    }

    private void login() {
        startProgressDialog();
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("by_convey_code", "registered");
        map.put("code", code);
        HttpUtils.ApiFunc(RetrofitManager.mApiService.login(map), new SubscriberListener<BaseResponse<LoginData>>() {
            @Override
            public void onSuccess(BaseResponse<LoginData> rewardListBaseResponse) {
                stopProgressDialog();
                Gson gson = new Gson();
                //保存登录数据。下次不需要登录
                App.setAccount(rewardListBaseResponse.getData());
                SPUtils.put(VerifyActivity.this,AppConfig.PERSONAL_DATA,gson.toJson(rewardListBaseResponse.getData()));
                MainActivity.start(VerifyActivity.this);
                EventBus.getDefault().post(AppConfig.CLOSE_LOGIN);
                finish();
            }

            @Override
            public void onFailer(String msg) {
                stopProgressDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mDownTimer) {
            mDownTimer.cancel();
        }
    }

    private CountDownTimer mDownTimer;
    private final long OFFEST = 1000;
    private final long COUNT_DOWN = 60 * 1000;
    String mLeftTip = "";

    private void startCountDown() {
        mLeftTip = getString(R.string.account_check_code_left_tip);
        mDownTimer = new CountDownTimer(COUNT_DOWN, OFFEST) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeCount.setText(String.format(mLeftTip, millisUntilFinished / OFFEST));
            }

            @Override
            public void onFinish() {
                mTimeCount.setEnabled(true);
                mTimeCount.setText("再次发送");
            }
        };
        mDownTimer.start();
    }

    private void sendMsg() {
        startProgressDialog();
        Map<String, String> map = new HashMap<>();
        map.put("address", phone);
        map.put("type", "login");
        HttpUtils.ApiFunc(RetrofitManager.mApiService.sendSms(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> rewardListBaseResponse) {
                stopProgressDialog();
                startCountDown();
            }

            @Override
            public void onFailer(String msg) {
                stopProgressDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }
}
